/**
 * 设备002 发送
 */
const ecUI = require('../../../utils/ecUI.js')
const ecBLE = require('../../../utils/ecBLE.js') 
const app = getApp(); 
let that;
Page({

  /**
   *指令：
      #sync,16:12:20     ——设备当前时间
      #tm_on,16:33        ——循环开始时间
      #tm_of,5:30         ——循环结束时间/开关时间
      #on_tm,1            ——循环开启时长
      #of_tm,1            ——循环关闭时长
      #dy_on,24           ——延时时间
      #dy_of,240          ——开关时长
      #mode,2             ——模式
      #init,1             ——初始状态
      #save               ——通用指令，保存设置。所有参数修改后，都需要保存。
      #reset              ——通用指令，复位。参数设置，保存后复位，设置参数生效。
      #read               ——通用指令，读取当前设定参数。
      #ver                ——通用指令，查询软件版本

   * 其中1-4条, 均回复与发送是一样的.
   */
   
  data: { 
    isFlashing: false,//绿灯闪烁开关状态
    flashInterval: null,//绿灯闪烁定时器
    receiveShow:false,//接收消息指令是否显示
    nowDate:null,
    nowTime:new Date().toLocaleTimeString(),
    modelList:[1,2,3,4,5,6,7,8,9],//模式设置0-9
    modelValue:1,//当前模式
    modelSetValue:0,//要设置为的模式值,modelValue-1
    cycleTimeHourList:[],//循环工作时间,工作时段0-23
    currentOnHour:'0',
    currentOfHour:'0',
    cycleTimeMinuteList:[],//循环工作时间,工作时段0-59
    currentOnMinute:'0',
    currentOfMinute:'0',
    switchDurationList:[],//开关时长/Switching duration,工作时段1-59
    currentSwitch:0,//模式2
    currentOn240Switch:0,//模式3-9
    currentOf240Switch:0,//模式3-9
    initStateList:[
      {"text":"关/OFF","value":0},
      {"text":"开/ON","value":1},],
    switchTimeDuration240List:[],
    switchTimeDuration24List:[],
    current240Switch:0,
    currentDelay24Switch:0,
    testTimeDurationList:[],
    currentTestTimeDuration:1,
    initState:1,
    initSetState:1, 
    modelSetImgPre:'http://www.zhiyankeji.com.cn/wechat/p002/',
    modelSetImg:'',
    scrollIntoView: 'scroll-view-bottom',
    receive:[],
    textRevData: '',
     time1:{day:0,hour:0,minute:0,second:0},
     time2:{day:0,hour:0,minute:0,second:0}, 
     device:{},
     errContent:'', 
     project:app.globalData.project1,
     testModeChecked:false,//测试模式，开关状态
    },
  
  onLoad(option) {   
    if(app.globalData.curentProject==2){
      this.setData({ 
        project:app.globalData.project2
      })
    };
    // 页面卸载时清除定时器，避免内存泄漏
     this.stopFlash();
     //调试接收数据样式
     // this.debugRecv()
    
    //设置系统时间
    this.setData({
      modelSetValue:this.data.modelValue-1,
      modelSetImg:this.data.modelSetImgPre+"1.png",
      nowDate:this.getNowDate(),
      nowTime:this.getNowTime()
    })
    //校准时间
    this.syncTime(); 
    setTimeout(function() {
      that.gerVer();
      setTimeout(function() {
        that.readTab();
      }, 300); // 500毫秒后执行
    }, 400); // 400毫秒后执行
    //初始化参数值
    var hours=[];
    for (let index = 0; index <= 23; index++) { 
      var recordIndex=  index < 10 ? '0' + index : index
      hours.push(recordIndex) 
    }
    var minutes=[] 
    for (let index = 0; index <= 59; index++) { 
      var recordIndex=  index < 10 ? '0' + index : index
      minutes.push(recordIndex)
    }
    var switchs=[] 
    for (let index = 1; index <= 59; index++) { 
      switchs.push(index)
    }
    var switchs240=[]
    for (let index = 1; index <= 240; index++) { 
      switchs240.push(index)
    }
    
    var switchs24=[]
    for (let index = 0; index <= 9; index++) { 
      switchs24.push(index)
    }
    
    testTimeDurationList
    var testTimeDurationList=[]
    for (let index = 1; index <= 255; index++) { 
      testTimeDurationList.push(index)
    }
    this.setData({
      cycleTimeHourList:hours,
      cycleTimeMinuteList:minutes,
      switchDurationList:switchs,
      switchTimeDuration24List:switchs24,
      switchTimeDuration240List:switchs240,
      testTimeDurationList:testTimeDurationList,
    })
    
    that=this;
    var deviceList=wx.getStorageSync('deviceList') 
    console.log(deviceList)
    if(deviceList){
      deviceList.forEach(item=>{
        if(item.deviceId==option.id){
          this.data.device=item;
        }
      })
      wx.setNavigationBarTitle({
        title:that.data.device.deviceName+'设置'
      });  
    }  
    // that=this;
    //设置字符集格式
    ecBLE.setChineseType(ecBLE.ECBLEChineseTypeGBK)
    //on disconnect
     this.connectChange();
     this.receiveData(); 
        //   //测试模式/Test Mode： #test,0,2或#test,1,2
        // // 1为测试开启，0为关闭
        // var str='#test,1,3'
        // if(str.startsWith('#test')){  
        //   console.log('接收到的test值：'+str)
        //   var testModeValue=parseInt(str.split(",")[1])     
        //   var currentTestTimeDuration=1  
        //   if(str.split(",")[2]!='undefined'&&str.split(",")[2]!='0'){
        //     var currentTestTimeDuration=parseInt(str.split(",")[2])    
        //   }  
        //   that.setData({ 
        //     currentTestTimeDuration:currentTestTimeDuration-1,
        //     testModeChecked:testModeValue==1?true:false,
        //   })  
        // }
        // //#ver版本号：#A9-20250626
        // str="#A9-20250626"
        // if(str.indexOf('-')){  
        //   console.log('接收到的ver值：'+str) 
        //   this.data.project.version=str.slice(1)
        //   that.setData({ 
        //     project:this.data.project, 
        //   })  
        // }
  },
  getNowDate(){ 
    const date  = new Date();
    const year = date.getFullYear().toString().padStart(4, "0");
    const month = (date.getMonth() + 1).toString().padStart(2, "0");
    const day = date.getDate().toString().padStart(2, "0");  
    return year + "-" + month + "-" + day;
  },
  getNowTime(){
    const date  = new Date();
    const hour = date.getHours().toString().padStart(2, "0");
    const minute = date.getMinutes().toString().padStart(2, "0");
    const second = date.getSeconds().toString().padStart(2, "0"); 
    return hour + ":" + minute + ":" + second;
  },
  ZeroFilling(data){
   var dataItems=  data.split(':');
   console.log(dataItems) 
   var hour=  parseInt(dataItems[0]) < 10 ? '0' + parseInt(dataItems[0]) : parseInt(dataItems[0]) 
   var minute=  parseInt(dataItems[1]) < 10 ? '0' + parseInt(dataItems[1]) : parseInt(dataItems[1]) 
   var second=  parseInt(dataItems[2]) < 10 ? '0' + parseInt(dataItems[2]) : parseInt(dataItems[2])  
   console.log(hour + ":" + minute + ":" + second)
   return hour + ":" + minute + ":" + second;
  },
 //校准时间
 syncTime(){ 
  this.toggleFlash();
      var syncTime='#sync,'+this.data.nowTime
      this.sendData(syncTime);
}, 
//获取版本号
gerVer(){  
     var verData='#ver'
     this.sendData(verData);
},
  //设置模式
  bindModelChange(e){ 
    console.log(e.detail.value)
      this.setData({
        modelSetValue:parseInt(e.detail.value)
      }) 
  },
  //设置初始状态
  bindInitStateChange(e){ 
    this.setData({
      initSetState:e.detail.value
    })
  },
    //设置开时间循环工作时间-时
  bindOnHourChange(e){ 
      // console.log("设置循环工作时间-时:"+e.detail.value,this.data.currentOfMinute)
        this.setData({
          currentOnHour:e.detail.value
        })  
    }, 
    //设置关时间循环工作时间-时
  bindOfHourChange(e){ 
    // console.log("设置循环工作时间-时:"+e.detail.value,this.data.currentOfMinute)
      this.setData({
        currentOfHour:e.detail.value
      })  
  }, 
  //设置开时间循环工作时间-分
  bindOfMinuteChange(e){ 
      // console.log("设置循环工作时间-分:"+this.data.currentOfHour,e.detail.value)
      this.setData({
        currentOfMinute:e.detail.value
      })  
  }, 
  //设置关时间循环工作时间-分
  bindOnMinuteChange(e){ 
    // console.log("设置循环工作时间-分:"+this.data.currentOfHour,e.detail.value)
    this.setData({
      currentOnMinute:e.detail.value
    })  
  },
//开关时长，开或关的持续时间，可设置1-59，单位秒
bindSwitchChange(e){ 
  // console.log(e.detail.value)
  this.setData({
    currentSwitch:e.detail.value
  })  
},
//开关时长， 模式3-9，开时长 持续时间，可设置1-240，单位秒
bindOn240SwitchChange(e){ 
  console.log(e.detail.value)
  this.setData({
    currentOn240Switch:e.detail.value
  })  
}, 
//开关时长， 模式3-9，关时长 持续时间，可设置1-240，单位秒
bindOf240SwitchChange(e){ 
  console.log(e.detail.value)
  this.setData({
    currentOf240Switch:e.detail.value
  })  
},  
//开关时长，开或关的持续时间，可设置1-240，单位秒
bind240SwitchChange(e){ 
  console.log(e.detail.value)
  this.setData({
    current240Switch:e.detail.value
  })  
},
//开关时长，开或关的持续时间，可设置1-240，单位秒
bindDelay24Change(e){ 
  console.log(e.detail.value)
  this.setData({
    currentDelay24Switch:e.detail.value
  })  
},

  //设置模式
bindTestTimeDurationChange(e){ 
    console.log(e.detail.value)
      this.setData({
        currentTestTimeDuration:parseInt(e.detail.value)
      }) 
  },
toggleFlash() {
  if (this.data.isFlashing) {
      this.stopFlash();
  } else {
      this.startFlash();
  }
},

startFlash() {
  this.setData({ isFlashing: true });
  const that = this;
  this.data.flashInterval = setInterval(() => {
      that.setData({
          isFlashing: !that.data.isFlashing
      });
  }, 100); // 每500ms切换一次状态
},

stopFlash() {
  clearInterval(this.data.flashInterval);
  this.setData({ isFlashing: false });
},
    // 调试用
    debugRecv(){ 
      console.log(2212)
      setTimeout(function() {
        console.log(12)
        var  receive=[{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'},{data:'1',time:'1111'}];
        var textData='';
        receive.forEach(item=>{
           textData+=item.time+'   '+item.data+ '\r\n'
        })
        that.setData({ 
          textRevData: textData
        })
        that.scrollToBottom();
      }, 1000); // 500毫秒后执行
    },
  scrollToBottom() {
    if (that.data.scrollIntoView === "scroll-view-bottom") {
      that.setData({ scrollIntoView: "scroll-view-bottom2" })
  } else {
    that.setData({ scrollIntoView: "scroll-view-bottom" })
  }
     
  },
  //清除接收信息
  clearReceveTab(){
     that.setData({
       receive:[],
      textRevData:''
     })
  },
  //重启定时器
  resetTab(){ 
    var sendData='#reset'
    that.sendData(sendData);
  },
  //从定时器读取
  getTab(){
    var sendData='#read'
    that.sendData(sendData);
  },
    //从定时器读取/read
    readTab(){
      this.toggleFlash();
      var sendData='#read'
      that.sendData(sendData);
    },   
    //向定时器保存/save
    saveTab(e){  
       this.toggleFlash();
        //模式设置
        if(e.currentTarget.dataset.obj=="modelSetting"){ 
            // console.log("modelSetting") 
          var sendModelData='#mode,'+that.data.modelList[that.data.modelSetValue] 
          var sendModelParamData='#init,'+that.data.initSetState
          that.sendData(sendModelData);   
          setTimeout(function() { 
            that.sendData(sendModelParamData); 
          }, 300); // 300毫秒后执行
        }
        //模式参数设置
        if(e.currentTarget.dataset.obj=="paramSetting"){  
          // console.log("paramSetting")
          //循环工作时间,关时间，模式1-9都有这个参数
          var sendCycleData='#tm_of,'+that.data.cycleTimeHourList[that.data.currentOfHour]+':'+that.data.cycleTimeMinuteList[that.data.currentOfMinute]
          that.sendData(sendCycleData); 
          //开关时长
          if(that.data.modelValue==1){  
            var sendSwitchData='#dy_of,'+that.data.switchDurationList[that.data.currentSwitch] 
            setTimeout(function() { 
              that.sendData(sendSwitchData);
            }, 200); // 200毫秒后执行
          }else if(that.data.modelValue==2){ 
            var sendSwitchData='#dy_of,'+that.data.switchTimeDuration240List[that.data.current240Switch]
            console.log(sendSwitchData) 
            var sendDelayData='#dy_on,';
            if(that.data.currentDelay24Switch==0){
              sendDelayData=sendDelayData+'0';
            }else{
              sendDelayData=sendDelayData+(parseInt(that.data.switchTimeDuration24List[that.data.currentDelay24Switch])*24+24);
            } 
            setTimeout(function() { 
              that.sendData(sendSwitchData);
              setTimeout(function() { 
                that.sendData(sendDelayData); 
              }, 200); // 100毫秒后执行
            }, 200); // 200毫秒后执行
          }else if(that.data.modelValue>=3&&that.data.modelValue<=9){
                //循环工作时间,开时间，模式1-9都有这个参数
                var sendCycleOnData='#tm_on,'+that.data.cycleTimeHourList[that.data.currentOnHour]+':'+that.data.cycleTimeMinuteList[that.data.currentOnMinute]
                var sendOnSwitchData='#on_tm,'+that.data.switchTimeDuration240List[that.data.currentOn240Switch]
                  // console.log(sendOnSwitchData) 
                  var sendOfSwitchData='#of_tm,'+that.data.switchTimeDuration240List[that.data.currentOf240Switch]
                  // console.log(sendOfSwitchData)  
                setTimeout(function() { 
                  that.sendData(sendCycleOnData); 
                  setTimeout(function() { 
                    that.sendData(sendOnSwitchData); 
                    setTimeout(function() { 
                      that.sendData(sendOfSwitchData); 
                    }, 100); // 100毫秒后执行
                  }, 100); // 100毫秒后执行
                }, 100); // 200毫秒后执行
          }
        } 
        //保存指令
        setTimeout(function() { 
          var sendData='#save'
          that.sendData(sendData);
          //重启定时器
          setTimeout(function() { 
            that.resetTab();      
          }, 200); // 500毫秒后执行
        }, 500); // 500毫秒后执行
    },    
//测试模式/TestMode 测试模式开关
listenerSwitch(e){   
  this.toggleFlash();     
      var sendTestTimeDuration='#test,'+that.data.testTimeDurationList[that.data.currentTestTimeDuration]
      that.sendData(sendTestTimeDuration);    
      setTimeout(function() {
        that.readTab();
      }, 400); // 500毫秒后执行
},    
  sendData(senddata){ 
      if (senddata.length === 0) {
          ecUI.showModal('提示', '请输入要发送的数据')
          return
      }
      let tempSendData = senddata.replace(/\n/g, '\r\n')
      if (tempSendData.length > 244) {
          ecUI.showModal('提示', '最多只能发送244字节')
          writeBLECharacteristicValue
      }
      console.log('发送:')
      console.log(tempSendData)
      ecBLE.writeBLECharacteristicValue(tempSendData, false)  
  },
  connectChange(){
        ecBLE.onBLEConnectionStateChange(() => {
          ecUI.showModal('提示', '设备断开连接')
      })
  },
  receiveData(){ 
    ecBLE.onBLECharacteristicValueChange((str, strHex) => {  
      //移除接收字符后边的换行符 
      str=str.slice(0, -1); 
      //校准时间 
      if(str.startsWith('#sync')||str.startsWith('#realtime')){
        console.log('接收到的sync/realtime值：'+str) 
        var nowTime= str.split(",")[1]; 
        nowTime=that.ZeroFilling(nowTime);
        that.setData({ 
          nowTime:nowTime
         })  
      }
      //模式/MODE：    
          if(str.startsWith('mode')||str.startsWith('#mode')){
            var index=str.indexOf('mode')
            console.log('接收到的mode值：'+str) 
            var modelValue=str.substring(5+index,6+index)
            var modelValueNew=(parseInt(modelValue)-1) 
            var modelSetImg=that.data.modelSetImgPre+modelValue+'.png'  
            // console.log(modelSetImg)
            that.setData({ 
              modelValue:modelValue,
              modelSetValue:modelValueNew,
              modelSetImg:modelSetImg
             }) 
          } 
          //初始状态/INIT： 
          if(str.startsWith('#init')){
            console.log('接收到的init值：'+str) 
            var initState=parseInt(str.split(",")[1]) 
            that.setData({ 
              initSetState:initState
             })  
          }
          //循环工作时间/Cycle working time： 
          if(str.startsWith('#tm_of')){
            var tmOfValue=str.split(",")[1] 
            var currentOfHour =parseInt(tmOfValue.split(":")[0])  
            var currentOfMinute =parseInt(tmOfValue.split(":")[1]) 
            that.setData({ 
              currentOfHour:currentOfHour,
              currentOfMinute:currentOfMinute
            })  
          }
          if(str.startsWith('#tm_on')){
            var tmOnValue=str.split(",")[1] 
            var currentOnHour =parseInt(tmOnValue.split(":")[0])  
            var currentOnMinute =parseInt(tmOnValue.split(":")[1]) 
            that.setData({ 
              currentOnHour:currentOnHour,
              currentOnMinute:currentOnMinute
            })  
          }
           //开关时长/Switching duration： 模式1/2数组包含值不一样而已
           if(str.startsWith('#dy_of')){ 
            console.log('接收到的dy_of值：'+str)
            var currentSwitch=parseInt(str.split(",")[1] )
            if(that.data.modelValue==1){ 
              that.setData({ 
                currentSwitch:currentSwitch-1
              })  
            }else if(that.data.modelValue==2){
              that.setData({ 
                current240Switch:currentSwitch-1
              })  
            }
          } 
          //延时时长/Delay duration： 
          if(str.startsWith('#dy_on')){  
            var currentDelay24Switch=0;
            if(parseInt(str.split(",")[1])>0){ 
              currentDelay24Switch=(parseInt(str.split(",")[1])-24)/24  
            } 
            console.log(currentDelay24Switch)
            that.setData({ 
              currentDelay24Switch:currentDelay24Switch,
              // currentDelay24Switch:1
            })  
          } 
          //  模式3-9数组包含值不一样而已
          if(str.startsWith('#on_tm')){ 
          console.log('接收到的on_tm值：'+str)
          var currentOn240Switch=parseInt(str.split(",")[1] )
          that.setData({ 
            currentOn240Switch:currentOn240Switch-1
          })  
        } 
        //延时时长/Delay duration： 
        if(str.startsWith('#of_tm')){  
          console.log('接收到的of_tm值：'+str)
          var currentOf240Switch=parseInt(str.split(",")[1])
          that.setData({ 
            currentOf240Switch:currentOf240Switch-1
          })  
        }
        //测试模式/Test Mode： #test,0,2或#test,1,2
        // 1为测试开启，0为关闭
        // var str='#test,1,3'
        if(str.startsWith('#test')){  
          console.log('接收到的test值：'+str)
          var testModeValue=parseInt(str.split(",")[1])     
          var currentTestTimeDuration=1  
          if(str.split(",")[2]!='undefined'&&str.split(",")[2]!='0'){
            var currentTestTimeDuration=parseInt(str.split(",")[2])    
          }  
          that.setData({ 
            currentTestTimeDuration:currentTestTimeDuration-1,
            testModeChecked:testModeValue==1?true:false,
          })  
        }
        //#ver版本号：#A9-20250626
        // str="#A9-20250626"
        if(str.indexOf('-')>0){  
          console.log('接收到的ver值：'+str) 
          this.data.project.version=str.slice(1)
          that.setData({ 
            project:this.data.project, 
          })  
        }
        if(str.startsWith('#save')||str.startsWith('#sync')||str.startsWith('#realtime')||str.startsWith('#test')){
          that.stopFlash();
        }
          // console.log(str)
        var receiveData={
          data:str,
          time:that.dateFormat('[hh:mm:ss,S]:', new Date())
        } 
        console.log('接收:');
        console.log(receiveData);
        that.data.receive.push(receiveData);
        console.log(that.data.receive);
        var textData='';
        that.data.receive.forEach(item=>{
           textData+=item.time+'   '+item.data+ '\r\n'
        })
       that.setData({
        //  receive: that.data.receive
        textRevData:textData, 
       })
       that.scrollToBottom();
     })
  },
  getModel(e){  
    let value = Number(e.detail.value); // 获取输入值并转换为数字 
    console.log(value)
    if(value>=11&&value<=14){
      value=value-10;
    }
    let min=e.currentTarget.dataset.min;
    let max=e.currentTarget.dataset.max;
    if (!Number.isFinite(value)) { // 如果输入的不是数字，可以重置或给出提示 
      value=e.detail.value.replace(/[^0-9]/g,'')
      this.setData({ modelValue: value }); // 重置输入值
      return;
    } 
    if (value > max) { // 如果输入值大于最大值，重置或提示
      this.setData({ modelValue: max}); // 设置最大值或给出提示
    }else if(value<min){ 
      this.setData({ modelValue: min}); // 设置最大值或给出提示
    }else {
      this.setData({ modelValue: value}); // 设置最大值或给出提示
    }  

 },
 getTime1(e){
  let value = Number(e.detail.value); // 获取输入值并转换为数字  
  let type=e.currentTarget.dataset.type;
  let min=e.currentTarget.dataset.min;
  let max=e.currentTarget.dataset.max; 
  if (!Number.isFinite(value)) { // 如果输入的不是数字，可以重置或给出提示
    value= e.detail.value.replace(/[^0-9]/g,'')
    // value=min; 
  } 
  if (value > max) { // 如果输入值大于最大值，重置或提示
    value=max;
  }   
  var time=this.data.time1;
  // time.day=Number(time.day)
  // time.hour=Number(time.hour)
  // time.minute=Number(time.minute)
  // time.second=Number(time.second)
  if(type=="day"){time.day=value;}
  if(type=="hour"){time.hour=value;}
  if(type=="minute"){time.minute=value;}
  if(type=="second"){time.second=value;}
  this.setData({
    time1:time
  })
 },
 getTime2(e){
  let value = Number(e.detail.value); // 获取输入值并转换为数字 
  let type=e.currentTarget.dataset.type;
  let min=e.currentTarget.dataset.min;
  let max=e.currentTarget.dataset.max; 
  if (!Number.isFinite(value)) { // 如果输入的不是数字，可以重置或给出提示
    value= e.detail.value.replace(/[^0-9]/g,'')
    // value=min; 
  } 
  if (value > max) { // 如果输入值大于最大值，重置或提示
    value=max;
  }    
  var time=this.data.time2;
  // time.day=Number(time.day)
  // time.hour=Number(time.hour)
  // time.minute=Number(time.minute)
  // time.second=Number(time.second)
  if(type=="day"){time.day=value;}
  if(type=="hour"){time.hour=value;}
  if(type=="minute"){time.minute=value;}
  if(type=="second"){time.second=value;}
  this.setData({
    time2:time
  })
 },
 clearInput(e){
   if(e.currentTarget.dataset.obj=="mode"){ 
       this.setData({
        modelValue:1
       }) 
   }else{
      let obj=e.currentTarget.dataset.obj.split(',');
      let timer=obj[0]
      let type=obj[1]
      console.log(timer,type);
      var time=this.data.time1; 
      if(timer=="time2"){
        time=this.data.time2; 
      }
      if(type=="day"){time.day=0;}
      if(type=="hour"){time.hour=0;}
      if(type=="minute"){time.minute=0;}
      if(type=="second"){time.second=0;}
      if(timer=="time1"){
        this.setData({
          time1:time
        })
      }else{
        this.setData({
          time2:time
        })
        }
      
  }   
 },
 btClearTap() {
  this.setData({ receiv: [] })
},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
      ecBLE.onBLEConnectionStateChange(() => { })
      ecBLE.onBLECharacteristicValueChange(() => { })
      ecBLE.closeBLEConnection()
  },
 dateFormat(fmt, date) {
  let o = {
      'M+': date.getMonth() + 1, //月份
      'd+': date.getDate(), //日
      'h+': date.getHours(), //小时
      'm+': date.getMinutes(), //分
      's+': date.getSeconds(), //秒
      'q+': Math.floor((date.getMonth() + 3) / 3), //季度
      S: date.getMilliseconds(), //毫秒
  }
  if (/(y+)/.test(fmt))
      fmt = fmt.replace(
          RegExp.$1,
          (date.getFullYear() + '').substr(4 - RegExp.$1.length)
      )
  for (var k in o)
      if (new RegExp('(' + k + ')').test(fmt)) {
          // console.log(RegExp.$1.length)
          // console.log(o[k])
          fmt = fmt.replace(
              RegExp.$1,
              RegExp.$1.length == 1
                  ? (o[k] + '').padStart(3, '0')
                  : ('00' + o[k]).substr(('' + o[k]).length)
          )
      }
  return fmt
},
})